/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaz;

import Negocio.ConexionMails;
import java.awt.Color;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Usuario
 */
public class Inicio extends javax.swing.JFrame {

    /**
     * Creates new form Inicio
     */
    static Principal cargar;
    HiloProgreso hilo;
    public Inicio() {
        initComponents();
        cargar=new Principal();
        iniciarSplash();
        //Creamos un objeto HiloProgreso al cual 
            //le pasamos por parámetro la barra de progreso
        hilo=new HiloProgreso(getProgreso(),this.cmdContinuar,this.lblMensaje);
        //Iniciamos el Hilo
        hilo.start();
        
        //Le damos tamaño y posición a nuestro Frame
        this.setLocationRelativeTo(null);
        //Liberamos recursos 
        hilo=null;
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        InterfazLogin = new javax.swing.JDialog();
        txtContraseña = new javax.swing.JPasswordField();
        cmdIngresar = new javax.swing.JButton();
        cmdCancelar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        Configuraciones = new javax.swing.JDialog();
        jLabel6 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        cmbServidores = new javax.swing.JComboBox();
        cmdGuardarConfiguraciones = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lblMensaje = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmdConfiguraciones = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        cmdContinuar = new javax.swing.JButton();
        progreso = new javax.swing.JProgressBar();

        cmdIngresar.setText("Ingresar");
        cmdIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdIngresarActionPerformed(evt);
            }
        });

        cmdCancelar.setText("Cancelar");
        cmdCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelarActionPerformed(evt);
            }
        });

        jLabel2.setText("Contraseña:");

        txtUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUsuarioActionPerformed(evt);
            }
        });

        jLabel3.setText("Usuario:");

        javax.swing.GroupLayout InterfazLoginLayout = new javax.swing.GroupLayout(InterfazLogin.getContentPane());
        InterfazLogin.getContentPane().setLayout(InterfazLoginLayout);
        InterfazLoginLayout.setHorizontalGroup(
            InterfazLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(InterfazLoginLayout.createSequentialGroup()
                .addGap(64, 64, 64)
                .addGroup(InterfazLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(InterfazLoginLayout.createSequentialGroup()
                        .addGroup(InterfazLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(38, 38, 38))
                    .addGroup(InterfazLoginLayout.createSequentialGroup()
                        .addComponent(cmdIngresar)
                        .addGap(18, 18, 18)))
                .addGroup(InterfazLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(InterfazLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtUsuario)
                        .addComponent(txtContraseña, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                    .addGroup(InterfazLoginLayout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(cmdCancelar)))
                .addContainerGap(83, Short.MAX_VALUE))
        );
        InterfazLoginLayout.setVerticalGroup(
            InterfazLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(InterfazLoginLayout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(InterfazLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(InterfazLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(InterfazLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdIngresar)
                    .addComponent(cmdCancelar))
                .addGap(30, 30, 30))
        );

        Configuraciones.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel6.setText("CONFIGURACION DE SERVIDOR");
        Configuraciones.getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 30, -1, -1));

        jLabel11.setText("-Servidor:");
        Configuraciones.getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 90, -1, -1));

        cmbServidores.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SendMail", "Postfix" }));
        Configuraciones.getContentPane().add(cmbServidores, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 90, 149, -1));

        cmdGuardarConfiguraciones.setText("Guardar...");
        cmdGuardarConfiguraciones.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdGuardarConfiguracionesActionPerformed(evt);
            }
        });
        Configuraciones.getContentPane().add(cmdGuardarConfiguraciones, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 160, -1, -1));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(102, 255, 0));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblMensaje.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblMensaje.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblMensaje.setText("Cargando Interfaces...");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(103, Short.MAX_VALUE)
                .addComponent(lblMensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(80, 80, 80))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(lblMensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 11, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 280, 400, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/logo.png"))); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 20, Short.MAX_VALUE)
                .addComponent(jLabel1))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        cmdConfiguraciones.setText("Configurar...");
        cmdConfiguraciones.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdConfiguracionesActionPerformed(evt);
            }
        });
        getContentPane().add(cmdConfiguraciones, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 60, 112, -1));

        jButton2.setText("Ayuda...");
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 100, 112, -1));

        cmdContinuar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Bien.png"))); // NOI18N
        cmdContinuar.setEnabled(false);
        cmdContinuar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdContinuarActionPerformed(evt);
            }
        });
        getContentPane().add(cmdContinuar, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 170, 78, -1));
        getContentPane().add(progreso, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 329, 586, 34));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdContinuarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdContinuarActionPerformed
        InterfazLogin.setSize(360, 220);
        InterfazLogin.setLocationRelativeTo(this);
        InterfazLogin.setModal(true);
        InterfazLogin.setVisible(true);
        
    }//GEN-LAST:event_cmdContinuarActionPerformed

    private void cmdConfiguracionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdConfiguracionesActionPerformed
        
        Configuraciones.setSize(500, 250);
        Configuraciones.setLocationRelativeTo(this);
        Configuraciones.setModal(true);
        Configuraciones.setVisible(true);
       
               
    }//GEN-LAST:event_cmdConfiguracionesActionPerformed

    private void cmdIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdIngresarActionPerformed

        // InterfazExAlumno interf = new InterfazExAlumno();
        String usuario = txtUsuario.getText();
        String contraseña = txtContraseña.getText();

        //System.out.println(contraseña);

        ConexionMails ingresar = new ConexionMails();
       // System.out.println(bandera);
        boolean band;
        try 
        {
            band = ingresar.Logear(usuario, contraseña);
            if (band == true)
           {
                    
                    cargar.setVisible(true);
                    cargar.setDefaultCloseOperation(0);
                    InterfazLogin.dispose();
                    this.dispose();
            
            } 
           else
           {
                JOptionPane.showMessageDialog(this, "Datos Incorrectos");
           }
        } catch (SQLException ex) {
            Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        

           


    }//GEN-LAST:event_cmdIngresarActionPerformed

    private void cmdCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelarActionPerformed
        // TODO add your handling code here:
        InterfazLogin.dispose();
    }//GEN-LAST:event_cmdCancelarActionPerformed

    private void txtUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUsuarioActionPerformed

   }//GEN-LAST:event_txtUsuarioActionPerformed

    private void cmdGuardarConfiguracionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdGuardarConfiguracionesActionPerformed

        //this.cmdActualizarActionPerformed(evt);
        String servidor =(String) this.cmbServidores.getSelectedItem();
        System.out.println(servidor);
        ConexionMails cambiar = new ConexionMails();
        try 
        {
            cambiar.cambiarServidor(servidor);
        } catch (SQLException ex) {
            Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
            //JOptionPane.showMessageDialog(this, "No se pudo conectar con el servidor: "+servidor);
        }
        Configuraciones.dispose();
        ///aqui falta programar



    }//GEN-LAST:event_cmdGuardarConfiguracionesActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() 
            {
                new Inicio().setVisible(true);
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDialog Configuraciones;
    private javax.swing.JDialog InterfazLogin;
    private javax.swing.JComboBox cmbServidores;
    private javax.swing.JButton cmdCancelar;
    private javax.swing.JButton cmdConfiguraciones;
    private javax.swing.JButton cmdContinuar;
    private javax.swing.JButton cmdGuardarConfiguraciones;
    private javax.swing.JButton cmdIngresar;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblMensaje;
    private javax.swing.JProgressBar progreso;
    private javax.swing.JPasswordField txtContraseña;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the progreso
     */
    public javax.swing.JProgressBar getProgreso() {
        return progreso;
    }
    public void iniciarSplash() 
    {
        this.getProgreso().setBorderPainted(true);
        this.getProgreso().setForeground(new Color(50, 50, 153, 100));
        this.getProgreso().setStringPainted(true);
    }
    
}
